﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;

namespace Grundy.Common
{
    public class SerializationHelper
    {
        private static List<string> _assemblies = new List<string> {"Grundy.Message", "Grundy.Client"};
        private static List<Type> _serializableTypes = LoadAssemblies();

        private static List<Type> LoadAssemblies()
        {
            _assemblies.ForEach(i =>
                                    {
                                        try
                                        {
                                            Assembly.Load(new AssemblyName(i));
                                        }
                                        catch
                                        {
                                        }
                                    });
            var serializableTypes = AppDomain.CurrentDomain.GetAssemblies()
                                    .Where(a => _assemblies.ContainsIgnoreCase(a.GetName().Name))
                                    .SelectMany(a => a.GetTypes())
                                    .Where(t => t.GetCustomAttributes(typeof(SerializableAttribute), false).Count() > 0 && t.IsClass)
                                    .ToList();
            return serializableTypes;
        }

        public IEnumerable<Type> GetSerializableTypes()
        {  
            return _serializableTypes;
        }
    }
}